package ucd.wirelessnetworkmonitor;

import ucd.wirelessnetworkmonitor.R;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.TrafficStats;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class WiFiNetworkActivity extends Activity {
	
	WifiManager wifiManager;
	WifiInfo wifiInfo;
	List<ScanResult> results;
	
	long up;
	long down;
	long zeroUp;
	long zeroDown;
	Handler aHandler;
	TextView uplink;
	TextView downlink;
	
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_wifi_network);

		wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		wifiInfo = wifiManager.getConnectionInfo();
		
		// start a scan and connect the callback to it
		// to prevent null pointer exception.
		results = wifiManager.getScanResults();
		
		// Gets info and puts it in second view's appropriate textView.
		String SSID = (wifiInfo.getSSID() == null) ? "Not Connected" : wifiInfo.getSSID();
		String BSSID = (wifiInfo.getBSSID() == null) ? "Not Connected" : wifiInfo.getBSSID();
		String RSSI = (BSSID.equals("Not Connected")) ? "Not Connected" : Integer.toString(wifiInfo.getRssi()) + " dBm";
		
		TextView ssidTextView = (TextView) findViewById(R.id.wiFiNetworkSSIDTextView);
		ssidTextView.setText("SSID: " + SSID);
		TextView bssidTextView = (TextView) findViewById(R.id.wiFiNetworkBSSIDTextView);
		bssidTextView.setText("BSSID: " + BSSID);
		TextView rssiTextView = (TextView) findViewById(R.id.wiFiNetworkRSSITextView);
		rssiTextView.setText("RSSI: " + RSSI);
		
		// Iterate over all available wireless ones and 
		// match its SSID to currently connected one.
		ScanResult currentResult = null;
		for(ScanResult temp : results){
			if(temp.BSSID.equals(wifiInfo.getBSSID())){
				currentResult = temp;
				break;
			}
		}
		
		// Set the security field.
		TextView securityTextView = (TextView) findViewById(R.id.wiFiNetworkSecurityTextView);
		if (currentResult != null) {
			String capabilities = currentResult.capabilities;
			String security;

			if (capabilities.contains("WPA"))
				security = "WPA";
			else if (capabilities.contains("WEP"))
				security = "WEP";
			else
				security = "None";
			
			securityTextView.setText("Security: " + security);
		}
		
		//This sets the link speed field.
		TextView linkSpeed = (TextView) findViewById(R.id.wiFiNetworkLinkSpeedTextView);
		linkSpeed.setText("Link Speed: " + wifiInfo.getLinkSpeed() + "Mbps");
		
		// Code for showing the data transferred and code to update it.
		zeroUp = TrafficStats.getTotalTxBytes();
		zeroDown = TrafficStats.getTotalRxBytes();
		up = TrafficStats.getTotalTxBytes() - zeroUp;
		down = TrafficStats.getTotalRxBytes() - zeroDown;
		
		uplink = (TextView) findViewById(R.id.wiFiNetworkUplink);
		downlink = (TextView) findViewById(R.id.wiFiNetworkDownlink);
		downlink.setText("Downlink: " + down + " bytes");
		uplink.setText("Uplink: " + up + " bytes");
		
		aHandler = new Handler();
		aHandler.post(update);
	}
	
	private Runnable update = new Runnable() {
    	public void run() {
    		up = TrafficStats.getTotalTxBytes() - zeroUp;
    		down = TrafficStats.getTotalRxBytes() - zeroDown;
    		downlink.setText("Downlink: " + down + " bytes");
    		uplink.setText("Uplink: " + up +" bytes");
    		aHandler.postDelayed(this, 1000);
    	}
    };
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wifi_network_options_menu, menu);
		return super.onCreateOptionsMenu(menu);
    }
    
    public boolean onMenuItemSelected(int id, MenuItem item) {
    	switch(item.getItemId()) {
    	case R.id.wifi_network_menu_refresh:
    		wifiManager.startScan();
    		return true;
    	case R.id.wifi_network_menu_about:
    		Intent intent = new Intent(this, AboutActivity.class);
    	    startActivity(intent);
    		return true;
    	default:
    		return super.onMenuItemSelected(id, item);
    	}
    }
}